#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

	connect(ui->brushPB, SIGNAL(pressed()), &mapper, SLOT(map()));
	mapper.setMapping(ui->brushPB, MyView::BrushMode);
	connect(&mapper, SIGNAL(mapped(int)), ui->graphicsView, SLOT(setMode(int)));

	connect(ui->rubberPB, SIGNAL(pressed()), &mapper, SLOT(map()));
	mapper.setMapping(ui->rubberPB, MyView::RubberyMode);
	connect(&mapper, SIGNAL(mapped(int)), ui->graphicsView, SLOT(setMode(int)));

	connect(ui->graphicsView, SIGNAL(painted(int,QPoint)), this, SIGNAL(painted(int,QPoint)));
	connect(ui->graphicsView, SIGNAL(sceneState(QTcpSocket*,QList<QPair<int,QPoint> >)),
			this, SIGNAL(sceneState(QTcpSocket*,QList<QPair<int,QPoint> >)));

	connect(ui->lockPB, SIGNAL(toggled(bool)), this, SLOT(setLock(bool)));
}

MainWindow::~MainWindow()
{
	delete ui;
}

void MainWindow::paint(int mode, QPoint point)
{
	ui->graphicsView->paint(mode, point, true);
}

void MainWindow::onAppStateNeeded(QTcpSocket *s)
{
	ui->graphicsView->onSceneStateNeeded(s);

	emit lockState(s, static_cast<bool>(ui->graphicsView->lockState()));
}

void MainWindow::setLock(int l)
{
	MyView::LockState ls = static_cast<MyView::LockState>(l);
	if ( ls == MyView::OtherLock )
	{
		ui->lockPB->setEnabled(false);
	}
	else
	{
		ui->lockPB->setEnabled(true);
	}
	ui->graphicsView->setLockState(ls);
}

void MainWindow::setLock(bool b)
{
	ui->graphicsView->setLockState( b ? MyView::MyLock : MyView::NoLock );

	emit locked(b);
}
